from flask import Blueprint, jsonify, render_template, request
from marshmallow import INCLUDE

from applications.common import curd
from applications.common.curd import model_to_dicts
from applications.common.utils import validate
from applications.common.utils.http import success_api, fail_api
from applications.extensions import db
from applications.models import Title
from applications.schemas import TitleOutSchema, TitleInSchema

title_bp = Blueprint('title', __name__, url_prefix='/admin/title')


@title_bp.get('/')
def main():
    return render_template('admin/title/main.html')


@title_bp.post('/data')
def data():
    titles = Title.query.order_by(Title.sort).all()
    title_data = model_to_dicts(schema=TitleOutSchema, data=titles)
    res = {
        "status": {"code": 200, "message": "默认"},
        "data": title_data
    }
    return jsonify(res)


@title_bp.get('/add')
def add():
    return render_template('admin/title/add.html')


@title_bp.get('/tree')
def get_tree_title():
    titles = Title.query.order_by(Title.sort).all()
    power_data = model_to_dicts(schema=TitleOutSchema, data=titles)
    res = {
        "status": {"code": 200, "message": "默认"},
        "data": power_data

    }
    return jsonify(res)


@title_bp.post('/save')
def save():
    try:
        req = request.json
        print(req)
        parent_id = req['parentId']
        title_name = req['titleName']
        sort = req['sort']
        status = req['status']
    except KeyError:
        return fail_api(msg='参数错误')
    title = Title(
        parent_id=parent_id,
        title_name=title_name,
        sort=sort,
        status=status
    )

    db.session.add(title)
    db.session.commit()
    return success_api(msg="成功")


@title_bp.get('/edit')
def edit():
    _id = request.args.get("titleId")
    title = curd.get_one_by_id(model=Title, id=_id)
    return render_template('admin/title/edit.html', title=title)


# 启用
@title_bp.put('/enable')
def enable():
    id = request.json.get('titleId')
    if id:
        enable = 1
        d = Title.query.filter_by(id=id).update({"status": enable})
        if d:
            db.session.commit()
            return success_api(msg="启用成功")
        return fail_api(msg="出错啦")
    return fail_api(msg="数据错误")


# 禁用
@title_bp.put('/disable')
def dis_enable():
    id = request.json.get('titleId')
    if id:
        enable = 0
        d = Title.query.filter_by(id=id).update({"status": enable})
        if d:
            db.session.commit()
            return success_api(msg="禁用成功")
        return fail_api(msg="出错啦")
    return fail_api(msg="数据错误")


@title_bp.put('/update')
def update():
    json = request.json
    validate.check_data(TitleInSchema(unknown=INCLUDE), json)
    id = json.get("titleId"),
    data = {
        "parent_id": validate.xss_escape(json.get("parentId")),
        "title_name": validate.xss_escape(json.get("titleName")),
        "sort": validate.xss_escape(json.get("sort")),
        "status": validate.xss_escape(json.get("status"))
    }
    d = Title.query.filter_by(id=id).update(data)
    if not d:
        return fail_api(msg="更新失败")
    db.session.commit()
    return success_api(msg="更新成功")


@title_bp.delete('/remove/<int:_id>')
def remove(_id):
    d = Title.query.filter_by(id=_id).delete()
    if not d:
        return fail_api(msg="删除失败")
    db.session.commit()
    return success_api(msg="删除成功")
